package com.kkb.filter;

import javax.servlet.*;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse) res;
        String url=request.getRequestURI();
        if ((!url.endsWith("login.html")&&request.getSession().getAttribute("username")==null)&&(url.endsWith(".html")||(!url.contains(".")&&!url.endsWith("admin/login")&&!url.endsWith("admin/code"))))
            response.sendRedirect("/login.html");
        else
            chain.doFilter(req,res);
    }

    @Override
    public void destroy() {

    }
}
